<%@ page import="cn.org.eshow.component.qiniu.QiniuConfig" %>
<script src="${dll}/fileupload/js/vendor/jquery.ui.widget.js"></script>
<script src="${dll}/fileupload/js/jquery.iframe-transport.js"></script>
<script src="${dll}/fileupload/js/jquery.fileupload.js"></script>

<script type="text/javascript">

    /**
     * 图片上传
     * @param _previewImg 浏览图标签ID
     * @param _upimg      接收图片input ID
     * @param _progress   进度条标签ID
     * @param _fileupload fileupload标签ID
     */
    function fileUp(_previewImg, _upimg, _progress, _fileupload) {
        //logo上传
        var url = 'http://up.qiniu.com/';
        var key = "";
        $("#" + _progress).parent().removeClass("none");
        $("#" + _fileupload).click();
        var time = new Date();
        key = time.getFullYear() + '/' + (time.getMonth() + 1) + '/' + time.getDate() + '/' + Date.parse(time);
        $('#' + _fileupload).fileupload({
            url: url,
            dataType: 'json',
            post: 'POST',
            formData: {
                key: key,
                token: "<%=cn.org.eshow.component.qiniu.QiniuUtil.uploadToken(QiniuConfig.BUCKET)%>"
            },
            done: function (e, data) {
                $("#" + _previewImg).attr("src", "<%=QiniuConfig.URL%>/" + key + "!small.jpg");
                $("#" + _upimg).val("<%=QiniuConfig.URL%>/" + key);
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#' + _progress).attr('style','width:'+progress + '%');
            }
        }).prop('disabled', !$.support.fileInput)
                .parent().addClass($.support.fileInput ? undefined : 'disabled');
    }


    /**
    * 多图上传
    * @param _bigId  大的id
    * @param _upimg      接收图片input ID
    * @param _fileupload fileupload标签ID
    */
    function fileUpGroup(_bigId, _upimg, _fileupload) {
        //logo上传
        var prog = $("#" + _bigId).find('.btn-addimgs em');
        console.log(prog);
        var url = 'http://up.qiniu.com/';
        var key = "";
        $("#" + _fileupload).click();
        var time = new Date();
        key = time.getFullYear() + '/' + (time.getMonth() + 1) + '/' + time.getDate() + '/' + Date.parse(time);
        $('#' + _fileupload).fileupload({
            url: url,
            dataType: 'json',
            post: 'POST',
            formData: {
                key: key,
                token: "<%=cn.org.eshow.component.qiniu.QiniuUtil.uploadToken(QiniuConfig.BUCKET)%>"
            },
            done: function (e, data) {
                var _img = '<span class="img"><i onclick="DelImg(this)">x</i><img src="<%=QiniuConfig.URL%>/' + key + '!small.jpg"  bigimg="<%=QiniuConfig.URL%>/'+key+'"/></span>'
                console.log(_img);
                var ob = $("#" + _bigId).find('.btn-addimgs').before(_img);
                $("#" + _bigId).attr("src", "<%=QiniuConfig.URL%>/" + key + "!small.jpg");

                $(prog).attr("style", 'width:0px;');
                var imgs = echeImgs(_bigId);
                $("#" + _upimg).val(imgs);
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $(prog).attr("style", 'width:'+progress + 'px;');
            }
        }).prop('disabled', !$.support.fileInput)
                .parent().addClass($.support.fileInput ? undefined : 'disabled');

    }

    function DelImg(ob) {
        var _this =  $(ob).parents('div');
        console.log($(_this).attr('id'));
        $(ob).parent('span.img').remove();
        var imgs = echeImgs($(_this).attr('id'))
        $("#upimgobj").val(imgs);
    }

    function echeImgs(id) {
        var imgs = '';
        $("#" + id).find('img').each(function(){
            imgs = imgs + $(this).attr('bigimg') + ',';
        });
        return imgs
    }

</script>